#include "logger_factory.h"
#include "infrastructure/logging/spdlog_logger.h"
#include <iostream>

namespace infrastructure {
namespace factories {

// 预定义的日志器名称常量
const std::string LoggerFactory::DEFAULT_LOGGER = "default";
const std::string LoggerFactory::USER_SERVICE_LOGGER = "UserService";
const std::string LoggerFactory::CONFIG_SERVICE_LOGGER = "ConfigService";
const std::string LoggerFactory::TASK_SERVICE_LOGGER = "TaskService";
const std::string LoggerFactory::VEHICLE_SERVICE_LOGGER = "VehicleService";
const std::string LoggerFactory::SCHEDULING_SERVICE_LOGGER = "SchedulingService";

LoggerFactory::LoggerFactory()
{
    // 使用真正的SpdlogLoggerFactory作为底层实现
    m_spdlogFactory = std::make_unique<infrastructure::logging::SpdlogLoggerFactory>();
    m_defaultLogger = m_spdlogFactory->getDefaultLogger();

    // 预注册常用的日志器
    m_loggers[DEFAULT_LOGGER] = m_defaultLogger;

    std::cout << "[LoggerFactory] 初始化完成 - 使用spdlog多日志器支持" << std::endl;
}

LoggerFactory::~LoggerFactory() = default;

std::shared_ptr<kernel::ILogger> LoggerFactory::getLogger(const std::string& loggerName)
{
    std::lock_guard<std::mutex> lock(m_mutex);

    auto it = m_loggers.find(loggerName);
    if (it != m_loggers.end()) {
        return it->second;
    }

    // 使用底层spdlog工厂创建新的日志器
    std::cout << "[LoggerFactory] 创建新日志器: " << loggerName << std::endl;
    auto newLogger = m_spdlogFactory->createLogger(QString::fromStdString(loggerName));
    m_loggers[loggerName] = newLogger;
    return newLogger;
}

std::shared_ptr<kernel::ILogger> LoggerFactory::getDefaultLogger()
{
    return m_defaultLogger;
}

std::shared_ptr<kernel::ILogger> LoggerFactory::createLogger(
    const std::string& loggerName,
    const std::string& logLevel)
{
    std::cout << "[LoggerFactory] 创建日志器: " << loggerName
              << ", 级别: " << logLevel << std::endl;

    // 使用底层spdlog工厂创建日志器（具体存储方式由基础设施层决定）
    auto logger = m_spdlogFactory->createLogger(QString::fromStdString(loggerName));

    // 设置日志级别
    if (logLevel == "debug") {
        logger->setLevel(kernel::ILogger::LogLevel::Debug);
    } else if (logLevel == "info") {
        logger->setLevel(kernel::ILogger::LogLevel::Info);
    } else if (logLevel == "warning" || logLevel == "warn") {
        logger->setLevel(kernel::ILogger::LogLevel::Warning);
    } else if (logLevel == "error") {
        logger->setLevel(kernel::ILogger::LogLevel::Error);
    } else if (logLevel == "critical") {
        logger->setLevel(kernel::ILogger::LogLevel::Critical);
    }

    return logger;
}

void LoggerFactory::registerLogger(const std::string& loggerName, std::shared_ptr<kernel::ILogger> logger)
{
    std::lock_guard<std::mutex> lock(m_mutex);

    if (!logger) {
        std::cout << "[LoggerFactory] 警告: 尝试注册空的日志器: " << loggerName << std::endl;
        return;
    }

    m_loggers[loggerName] = logger;
    std::cout << "[LoggerFactory] 注册日志器: " << loggerName << std::endl;
}

bool LoggerFactory::hasLogger(const std::string& loggerName) const
{
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_loggers.find(loggerName) != m_loggers.end();
}

} // namespace factories
} // namespace infrastructure